package cn.zktr.Personnel.model.service.tanyali;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;

import cn.zktr.Personnel.model.dao.tanyali.EmaOutDao;
import cn.zktr.Personnel.model.dao.tanyali.EmaQsbDao;
import cn.zktr.Personnel.model.dao.tanyali.EmaTSDao;
import cn.zktr.Personnel.model.dao.tanyali.EmaTmDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsEmpDao;
import cn.zktr.Personnel.model.mappers.tanyali.EmaOutMapper;
import cn.zktr.Personnel.model.mappers.tanyali.EmaQsbMapper;
import cn.zktr.Personnel.model.mappers.tanyali.EmaTmMapper;
import cn.zktr.Personnel.model.mappers.tanyali.EmaTsMapper;
import cn.zktr.Personnel.model.pojos.PerEmaOut;
import cn.zktr.Personnel.model.pojos.PerEmaQsb;
import cn.zktr.Personnel.model.pojos.PerEmaTm;
import cn.zktr.Personnel.model.pojos.PerEmaTs;
import cn.zktr.Personnel.model.pojos.PerPmsEmp;

@Service
@Transactional(rollbackOn=Exception.class)
public class EmaTmService {
	@Autowired
	private EmaTmDao tmdao;
	@Autowired
	private EmaTSDao tsdao;
	
	@Autowired
	private PmsEmpDao empdao;

	@Autowired
	private EmaOutDao outdao;
	
	@Autowired
	private EmaQsbDao qsbdao;
	
	@Autowired
	private EmaQsbMapper qsbM;

	@Autowired
	private EmaTmMapper tmmer;
	
	
	@Autowired
	private EmaOutMapper outmer;
	
	public Iterable<PerEmaTm> selectFebAll(){
		return tmdao.findAll();
	}
	
	public void updateTM(Integer tmid,String tk,String zsz,String nyd,String TIMU,String fs,String type,String DAAN) {
		 PerEmaTm tm=new PerEmaTm();
		 if(tmid!=null) {
			 tm.setTmzid(tmid);
		 }
		 PerEmaQsb findBytk = findBytk(tk);
		 if(findBytk!=null) {
			 tm.setPerEmaQsb(findBytk);
		 }else {
			 update(tk);
			 tm.setPerEmaQsb(findBytk(tk));
		 }
          tm.setTmsub(TIMU);
          tm.setTmnyd(nyd);	
          tm.setTmtype(type);
          tm.setTmzsd(zsz);
          tm.setTmfs(Integer.valueOf(fs.trim()));
          tm.setTmbda(DAAN);
          tmdao.save(tm);
	}
	public PerEmaQsb findBytk(String qsbbank) {
//		return qsbM.tikuchaxunName(qsbbank);
		return qsbdao.findByQsbbank(qsbbank);
	}
	public void update(String tk) {
		PerEmaQsb Qsbbank = new PerEmaQsb();
		Qsbbank.setQsbbank(tk);
		qsbdao.save(Qsbbank);
	}
	
	public void updateSJ(Integer empid,String mc,Integer zf) {
		 PerEmaOut out=new PerEmaOut();
		 out.setOutname(mc);
		 out.setPerPmsEmp(empdao.findById(empid).get());
		 out.setOutzonf(zf);
		 outdao.save(out);
	} 
	
	public PerEmaOut selectOutMaxId() {
		return outmer.selectOutMaxId();
	}
	
	public void updatetmSJ(Integer tmid) {
		PerEmaTs ts = new PerEmaTs();
		ts.setPerEmaOut(selectOutMaxId());
		ts.setPerEmaTm(tmdao.findById(tmid).get());
		tsdao.save(ts);
	}
	
	
	public PerEmaQsb findBytk2(String tk) {
		return qsbM.tikuchaxunName(tk);
	}
	public void update2(String tk) {
		PerEmaQsb Qsbbank = new PerEmaQsb();
		Qsbbank.setQsbbank(tk);
		qsbdao.save(Qsbbank);
	}
	
	public PerPmsEmp findBycjr(String cjr) {
		return qsbM.chujuanrenName(cjr);
	}
	public void updatecjr(String cjr) {
		PerPmsEmp empname = new PerPmsEmp();
		empname.setEmpname(cjr);
		empdao.save(empname);
	}
	
	public void sjxingzengtm(Integer tmid) {
		PerEmaTm tm=new PerEmaTm();
		 if(tmid!=null) {
			 tm.setTmzid(tmid);
		 }
	}
	/**
	 *return 分数字段
	 */
	public JSONArray fenshuselect() {
		return tmmer.fenshuselect();
		
	}
	/**
	 *return 试题字段
	 */
	public JSONArray shitiselect() {
		return tmmer.shitiselect();
		
	}
	
	/**
	 *return 难易度字段
	 */
	public JSONArray nanyiduselect() {
		return tmmer.nanyiduselect();
		
	}
	/**
	 *return 知识点字段
	 */
	public JSONArray zhishidianselect() {
		return tmmer.zhishidianselect();
		
	}
	/**
	 *return 题库字段
	 */
	public JSONArray tikuselect() {
		return tmmer.tikuselect();
		
	}
	
	/**
	 *return 试卷名称字段
	 */
	public JSONArray shijuanselect() {
		return tmmer.shijuanselect();
		
	}
	
	/**
	 *return 出卷人字段
	 */
	public JSONArray chujuanrenselect() {
		return tmmer.chujuanrenselect();
		
	}
	
	/**
	 *return 考试时长字段
	 */
	public JSONArray shichangselect() {
		return tmmer.shichangselect();
		
	}
	
	/**
	 *return 考试时长字段
	 */
	public JSONArray zongfenselect() {
		return tmmer.zongfenselect();
		
	}
	
}
